'''
Created on 08/04/2009

@author: eh01
'''
from im.ds.dsstub import DSStub
from im.event import eventconstants
from im.event.eventmanager import EventManager
from im.handler.httperrorhandler import HTTPErrorHandler
from im.handler.httprequestdeletenodemessagehandler import \
    HTTPRequestDELETENodeMessageHandler
from im.handler.httprequestgetadminhandler import HTTPRequestGETAdminHandler
from im.handler.httprequestgetindexhandler import HTTPRequestGETIndexHandler
from im.handler.httprequestgetnodehandler import HTTPRequestGETNodeHandler
from im.handler.httprequestgetnodelastconnectedhandler import \
    HTTPRequestGETNodeLastConnected
from im.handler.httprequestgetnodemessagehandler import \
    HTTPRequestGETNodeMessageHandler
from im.handler.httprequestgetnodemessageshandler import \
    HTTPRequestGETNodeMessagesHandler
from im.handler.httprequestgetstaticpagehandler import \
    HTTPRequestGETStaticPageHandler
from im.handler.httprequestpostflushmemcachehandler import \
    HTTPRequestPOSTFlushMemcacheHandler
from im.handler.httprequestputnodemessagehandler import \
    HTTPRequestPUTNodeMessageHandler
from im.handler.imhttpdelegaterequesthandler import IMHTTPDelegateRequestHandler
from im.handler.imhttpresponsehandler import IMHTTPResponseHandler
from im.imconfig import IMConfig
from im.webserver.imhttpserver import IMHTTPServer
import sys

http_server_port_number = None
class Initializer(object):

    __dsstub = None
    
    def initialize_db_stub(self):
        self.__dsstub = DSStub()
        imconfig = IMConfig(config_file_absolute_path)
        self.__dsstub.set_IM_Config(imconfig)
        
    def initialize_event_manager(self):
        # override the default get_eventHandlers method at a class level for testing purposes
        instancemethodEventHandlers = type(EventManager.get_eventHandlers)
        EventManager.get_eventHandlers = instancemethodEventHandlers(get_eventHandlers_override, None, EventManager)
        eventManager = EventManager()
        eventManager.start()

    def initialize_web_server(self):
        self.__server = IMHTTPServer(self.__dsstub.get_http_server_port())
        self.__server.start()   

    def shutdown_event_manager(self):
        EventManager().stop()
        
    def shutdown_web_server(self):
        self.__server.stop()
                            
    def initialize(self):
        self.initialize_db_stub()
        self.initialize_event_manager()
        self.initialize_web_server()
        print 'Initialization Complete'
                
    def shutdown(self):
        self.shutdown_event_manager()
        self.shutdown_web_server()      
        print 'Shutdown Complete'

def get_eventHandlers_override(self):
    im_http_delegagte_request_handler = IMHTTPDelegateRequestHandler()
    http_request_get_static_page_handler = HTTPRequestGETStaticPageHandler()
    im_http_response_handler = IMHTTPResponseHandler()
    http_error_handler = HTTPErrorHandler()
    http_request_get_index_handler = HTTPRequestGETIndexHandler()
    http_request_get_node_messages_handler = HTTPRequestGETNodeMessagesHandler()
    http_request_get_node_message_handler = HTTPRequestGETNodeMessageHandler()
    http_request_put_node_message_handler = HTTPRequestPUTNodeMessageHandler()
    http_request_delete_node_message_handler = HTTPRequestDELETENodeMessageHandler()
    http_request_get_admin_handler = HTTPRequestGETAdminHandler()
    http_request_get_node_last_connected_handler = HTTPRequestGETNodeLastConnected()
    http_request_get_node_handler = HTTPRequestGETNodeHandler()
    http_request_post_flush_memcache_handler = HTTPRequestPOSTFlushMemcacheHandler()
    eventHandlers = {eventconstants.HTTP_REQUEST_EVENT_TYPE:im_http_delegagte_request_handler,
                     eventconstants.HTTP_REQUEST_GET_STATIC_PAGE:http_request_get_static_page_handler,
                     eventconstants.HTTP_RESPONSE_EVENT_TYPE:im_http_response_handler,
                     eventconstants.HTTP_ERROR_EVENT_TYPE:http_error_handler,
                     eventconstants.HTTP_REQUEST_GET_INDEX_EVENT_TYPE:http_request_get_index_handler,
                     eventconstants.HTTP_REQUEST_GET_NODE_MESSAGES_EVENT_TYPE:http_request_get_node_messages_handler,
                     eventconstants.HTTP_REQUEST_GET_NODE_MESSAGE_EVENT_TYPE:http_request_get_node_message_handler,
                     eventconstants.HTTP_REQUEST_PUT_NODE_MESSAGE_EVENT_TYPE:http_request_put_node_message_handler,
                     eventconstants.HTTP_REQUEST_DELETE_NODE_MESSAGE_EVENT_TYPE:http_request_delete_node_message_handler,
                     eventconstants.HTTP_REQUEST_GET_ADMIN_EVENT_TYPE:http_request_get_admin_handler,
                     eventconstants.HTTP_REQUEST_GET_NODE_LAST_CONNECTED_EVENT_TYPE:http_request_get_node_last_connected_handler,
                     eventconstants.HTTP_REQUEST_GET_NODE_EVENT_TYPE:http_request_get_node_handler,
                     eventconstants.HTTP_REQUEST_POST_FLUSH_MEMCACHE_EVENT_TYPE:http_request_post_flush_memcache_handler
                    }        
    return eventHandlers

if __name__ == '__main__':
    
    config_file_absolute_path = sys.argv[1]
    initializer = None
    try:
        pass
        initializer = Initializer()
        initializer.initialize()
        if DSStub().is_allow_keyboard_interrupt():
            # wait for KeyboardInterrupts
            sys.stdin.read()
            print 'Stopping'
    except KeyboardInterrupt:
        initializer.shutdown()
        
        